package com.douyu.bolt;

import java.util.HashMap;
import java.util.Map;

import backtype.storm.task.OutputCollector;
import backtype.storm.task.TopologyContext;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.base.BaseRichBolt;
import backtype.storm.tuple.Fields;
import backtype.storm.tuple.Tuple;
import backtype.storm.tuple.Values;

/**
 * @Auther: xiaoyuan
 * @Date: 2019/6/19
 * @Description:
 */
public class WordCountBolt extends BaseRichBolt {

    private OutputCollector collector;

    private HashMap<String,Long> counts = null;

    public void prepare(Map map, TopologyContext topologyContext, OutputCollector outputCollector) {

        this.collector = outputCollector;
        this.counts = new HashMap<String, Long>();

    }

    public void execute(Tuple tuple) {

        String word = tuple.getStringByField("word");
        Long count = this.counts.get(word);
        if(count==null){
            count=0L;
        }
        count++;
        this.counts.put(word,count);
        this.collector.emit(tuple,new Values(word,count));
        this.collector.ack(tuple);

    }

    public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
        outputFieldsDeclarer.declare(new Fields("word","count"));
    }
}
